package cn.com.dev.temp;

import cn.com.kh.KHUtil;
import cn.com.kh.TxConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @Title: cn.com.dev-DevTao
 * @Description:ngsapitask-ph 系统API接口开发Demo，重点关注业务逻辑部分
 * @Author: lijun
 * @Date 2021-12-03 10:23
 */
public class DevTao {
    private static final Log log = LogFactory.getLog(DevTao.class);
    private static String APPID = "100002";
    private static String APPKEY = "E4uX9c65e5h7A5K5DA6rw6JL";

    public static void main(String[] args) throws Exception {
        // 获取数据库操作对象
        JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());

        /*String getContSQL = getContentDataSQL(jt, "MB164258075423839", "04FAA909-E121-6FE7-08A4-5FF51D0EC845");
        log.debug("模拟认证回调 返回✅:" + getContSQL);*/

        String sql1 = "select Code_ID,Code_Name,PCode_ID from c_codetables";
        String sql2 = "update2 c_codetables set Remark = '' where Remark = '123123213'";

        jt.execute(sql1);
        jt.execute(sql2);

    }


    public static String getContentDataSQL(JdbcTemplate jt, String templateId, String rec_Guid) {

        JSONArray conArr = new JSONArray();

        String ModelID = templateId;
        String contractid = templateId;
        log.info("【合同模板id】：" + ModelID);

        String sqls = "select fdd_key, work_key, Exp_Rule, Exp_Value from fdd_contract_key where contractfileid='" + ModelID + "'";
        List<Map<String, Object>>  resultlist = jt.queryForList(sqls);
        if (resultlist.size() == 0) {
            JSONObject curJson = new JSONObject();
            curJson.put("deslcode", -1);
            curJson.put("dealinfo", "当前模板没有维护key：" + ModelID);
            conArr.add(curJson);
            log.debug("【当前模板没有维护key】：" + ModelID);
        }

        String objparam = "";
        for (int i = 0; i < resultlist.size(); i++) {
            String work_keyt = KHUtil.GetMapV(resultlist.get(i),"work_key");
            String Exp_Rule = KHUtil.GetMapV(resultlist.get(i),"Exp_Rule");
            String Exp_Value = KHUtil.GetMapV(resultlist.get(i),"Exp_Value");

            if (work_keyt == "workorderworkorderid_zl") {
                work_keyt = "case when '" + contractid + "' !='' then '" + contractid + "' when workorderworkorderid_zl is not null and workorderworkorderid_zl !='' then workorderworkorderid_zl else '/' end";
            } else {
                if (work_keyt == "zl_leaseapplicationtimestart" || work_keyt == "zl_leaseapplicationtimesend" || work_keyt == "stagesttime1" || work_keyt == "stagesttime2" || work_keyt == "stagesttime3" || work_keyt == "leaseapplicationtimestart" || work_keyt == "leaseapplicationtimesend" || work_keyt == "releasetime" || work_keyt == "contract_signtime") {

                    // 日期时间格式特殊处理
                    work_keyt = (work_keyt == "zl_leaseapplicationtimestart" || work_keyt == "zl_leaseapplicationtimesend" || work_keyt == "stagesttime1" || work_keyt == "stagesttime2" || work_keyt == "stagesttime3" || work_keyt == "leaseapplicationtimestart" || work_keyt == "leaseapplicationtimesend" || work_keyt == "releasetime" || work_keyt == "contract_signtime") ? "if (ISNULL(" + work_keyt + ")=1 or LENGTH(TRIM(" + work_keyt + "))=0,'/'," + "DATE_FORMAT(" + work_keyt + ",'%Y年%m月%d日')" + ")" : "if (ISNULL(" + work_keyt + ")=1 or LENGTH(TRIM(" + work_keyt + "))=0,'/'," + work_keyt + ")";
                } else if (!KHUtil.isNull(Exp_Rule)) {
                    // 如果模版维护了默认值正则,则用默认值正则
                    work_keyt = "If('" + Exp_Rule + "' = '', " + work_keyt + ", If(" + work_keyt + " Regexp '" + Exp_Rule + "' = 1, '" + Exp_Value + "', " + work_keyt + "))";

                } else {

                    // 其他情况下全部为空时默认/
                    work_keyt = "if (ISNULL(" + work_keyt + ")=1 or LENGTH(TRIM(" + work_keyt + "))=0,'/'," + work_keyt + ")";

                }
            }
            log.debug("合同模版字段映射工单Tabc字段预处理： " + work_keyt + "  法大大Key： " + resultlist.get(i).get("fdd_key"));
            objparam = objparam + "," + work_keyt + " as '" + resultlist.get(i).get("fdd_key") + "'";
        }

        objparam = objparam.startsWith(",") ? objparam.substring(1) : objparam;
        log.debug("【objparam】：" + objparam);

        return objparam.toString();
    }

}
